# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>

_realname=importlib-metadata
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=0.23
pkgrel=2
pkgdesc="Read metadata from Python packages (mingw-w64)"
arch=('any')
url='https://importlib-metadata.readthedocs.io/'
license=('Apache')
depends=("${MINGW_PACKAGE_PREFIX}-python3"
         "${MINGW_PACKAGE_PREFIX}-python3-contextlib2"
         "${MINGW_PACKAGE_PREFIX}-python3-pathlib2"
         "${MINGW_PACKAGE_PREFIX}-python3-zipp")
makedepends=("${MINGW_PACKAGE_PREFIX}-python3-setuptools")
checkdepends=("${MINGW_PACKAGE_PREFIX}-python3-pip"
              "${MINGW_PACKAGE_PREFIX}-python3-pytest-runner"
              "${MINGW_PACKAGE_PREFIX}-python3-wheel")
source=("${_realname}-${pkgver}.tar.gz"::"https://files.pythonhosted.org/packages/source/${_realname::1}/${_realname//-/_}/${_realname//-/_}-${pkgver}.tar.gz")
sha512sums=('56594dfd67733842d83547770a09e12b4e4e3c000b7c9743206e13e4629906bb7271065e03c387f5114bac7f673fc17594d2ef90af23cd34b7ededecaf3fd47a')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

del_file_exists() {
  for _fname in "$@"
  do
    if [ -f $_fname ]; then
      rm -rf $_fname
    fi
  done
}
# =========================================== #

prepare() {
  cd "${srcdir}"
  for builddir in python3-build-${CARCH}; do
    rm -rf ${builddir} | true
    cp -r "importlib_metadata-${pkgver}" "${builddir}"
  done
  # Set version for setuptools_scm
  export SETUPTOOLS_SCM_PRETEND_VERSION=${pkgver}
}

# Note that build() is sometimes skipped because it's done in 
# the packages setup.py install for simplicity if you can do so.
# but sometimes, you want to do a check before install which would
# also trigger the build.
build() {
  msg "Python 3 build for ${CARCH}"  
  cd "${srcdir}/python3-build-${CARCH}"
  ${MINGW_PREFIX}/bin/python3 setup.py build
}

check() {
  msg "Python 2 test for ${CARCH}"
  cd "${srcdir}/python2-build-${CARCH}"
  ${MINGW_PREFIX}/bin/python2 setup.py pytest
}

package() {
 cd "${srcdir}/python3-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python3 setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python3-${_realname}/LICENSE"
}
